package controller.admin;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import pojos.common.ReplyMessage;
import pojos.subject.UserRelationshipManager;
import service.SubjectInfoService;

import javax.annotation.Resource;

/**
 * 管理员课程管理控制器
 */
@Controller
@RequestMapping(value = {"/admin"})
public class AdminForSubjectController {

    @Resource(name = "subjectService")
    private SubjectInfoService subjectInfoService;

    /**
     * 修改用户关系
     */
    @PostMapping("/frame/updateRelationship")
    @ResponseBody
    public ReplyMessage alterUserRelationship(@RequestParam(value = "reqId",required = false) Integer reqId,
                                              @RequestParam(value = "teacherId",required = false) Integer teacherId,
                                              @RequestParam(value = "parentsId",required = false) Integer parentsId,
                                              @RequestParam(value = "comment",required = false) Integer isEnable,
                                              @RequestParam(value = "isEnable",required = false) String comment){
        ReplyMessage replyMessage = new ReplyMessage();
        UserRelationshipManager manager = new UserRelationshipManager();

        if(reqId == null){
            replyMessage.setStatus(400);
            replyMessage.setInfo("未知修改属性，请尝试刷新界面后重新提交！");
            return replyMessage;
        }
        if(teacherId == null){
            replyMessage.setStatus(400);
            replyMessage.setInfo("未知的教师信息，请选择教师后提交！");
            return replyMessage;
        }
        if(parentsId == null){
            replyMessage.setStatus(400);
            replyMessage.setInfo("未知的家长信息，请选择家长信息后提交！");
            return replyMessage;
        }

        if(comment != null){
            if(comment.length() > 250){
                replyMessage.setStatus(400);
                replyMessage.setInfo("备注不能超过250个字符！");
                return replyMessage;
            }
            manager.setComment(comment);
        }

        if(isEnable != null){
            manager.setIsEnable(isEnable);
        }

        manager.setParentsId(parentsId);
        manager.setTeacherId(teacherId);
        manager.setRelationId(reqId);

        return subjectInfoService.updateRelationship(manager);
    }

    /**
     * 查询用户当天课程信息
     */
    @PostMapping("/frame/query-user-current-class-info")
    @ResponseBody
    public ReplyMessage queryUserCurrentClassInfo(){
        return subjectInfoService.queryUserCurrentClassInfo();
    }
}
